package com.lovo.service;

import com.lovo.dao.OrderDao;
import com.lovo.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class MovieService {
    @Autowired
    OrderDao orderDao;

    /**
     * 后台接收前台传入的订单数据并进行同步处理
     * 使用ack进行消费者事务管理
     *
     * @param orderEntity //     * @param channel
     *                    //     * @param tag
     */
    @RabbitListener(queues = "queueOrder")
    public void getOrder(OrderEntity orderEntity, Channel channel, @Header(AmqpHeaders.DELIVERY_TAG) long tag) throws IOException {
        if (null != orderEntity) {
            //orderDao.save(orderEntity);
            //后台接收订单信息成功
            System.out.println("or=" + orderEntity);
            orderDao.insert(orderEntity);
            //提交确认
            channel.basicAck(tag, false);
        }
    }

    @RabbitListener(queues = "queueDead")
    public void getOrder(OrderEntity orderEntity) {
        System.out.println("进入死信队列");
        System.out.println("dead=" + orderEntity);
    }
}
